package com.onlinemovies.repository.extension;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;



import java.util.Set;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.onlinemovies.entity.BookingDetails;
import com.onlinemovies.entity.MovieDetails;
import com.onlinemovies.entity.ShowDetails;
import com.onlinemovies.repository.AbstractRepository;


@Repository("movieRepository")
public class MovieRepository extends AbstractRepository<MovieDetails> implements Serializable{

	@SuppressWarnings("unchecked")
	public List<MovieDetails> getAll() {
	        return entityManager
	        		.createQuery("SELECT m FROM MovieDetails m ORDER BY m.moviename")
	        		.getResultList();
	    }

	public Set<MovieDetails> getMovieListAgainstHallid(Integer hallid) {
		Query createQuery = entityManager
        		.createQuery("SELECT m FROM MovieDetails m where m.hallDetails.id=?1 and m.status='A'")
        		.setParameter(1, new Long(hallid));
		Set<MovieDetails> set=new HashSet<MovieDetails>();
		 List<MovieDetails> resultList = createQuery.getResultList();
		 set.addAll(resultList);
		
		 
		return set;
	}

	public Set<ShowDetails> getShowListAgainstMovieid(Integer movieid) {
		Query createQuery = entityManager
        		.createQuery("SELECT m FROM ShowDetails m where m.movieDetails.id=?1")
        		.setParameter(1, new Long(movieid));
		Set<ShowDetails> set=new HashSet<ShowDetails>();
		 List<ShowDetails> resultList = createQuery.getResultList();
		 set.addAll(resultList);
		
		 
		return set;
	}

	public String findPriceForMovie(Integer showid) {
		Query createQuery = entityManager
        		.createQuery("SELECT m FROM ShowDetails m where m.id.id=?1")
        		.setParameter(1, new Long(showid));
		List<ShowDetails> resultList = createQuery.getResultList();
		String price="0.00";
		if(resultList!=null && resultList.size()>0)
			price=resultList.get(0).getPrice();
		return price;
	}

	

	public boolean checkAvaliableSeat(BookingDetails bookingDetails) {
		boolean flag=false;
		Query createQuery = entityManager
        		.createQuery("SELECT m FROM ShowDetails m where m.id.id=?1")
        		.setParameter(1, new Long(bookingDetails.getShowDetails().getId()));
		List<ShowDetails> resultList = createQuery.getResultList();
		long noofbook=0;
		Query createQuery1 = entityManager
        		.createQuery("SELECT sum(b.noofseat) FROM BookingDetails b where b.showDetails.id=?1 and b.hallDetails.id=?2  and b.movieDetails.id=?3"
        				+ "  and b.showdate=?4")
        		.setParameter(1, new Long(bookingDetails.getShowDetails().getId()))
        		.setParameter(2, new Long(bookingDetails.getHallDetails().getId()))
        		.setParameter(3, new Long(bookingDetails.getMovieDetails().getId()))
        		.setParameter(4, bookingDetails.getShowdate());
		Object res = createQuery1.getSingleResult();
		if(null != res) {
			noofbook = (Long) res;
		}
		
		noofbook=noofbook+bookingDetails.getNoofseat();
		if(resultList!=null && resultList.size()>0){
			int totalseat=0;
			if(resultList.get(0).getNoofseat()!=null && resultList.get(0).getNoofseat().isEmpty()==false)
				totalseat=Integer.parseInt(resultList.get(0).getNoofseat());
			if(totalseat>=noofbook){
								
				BigDecimal bigDecimal=new BigDecimal(bookingDetails.getNoofseat());
				BigDecimal bigDecimal1=new BigDecimal(resultList.get(0).getPrice());
				bookingDetails.setPrice(bigDecimal.multiply(bigDecimal1).doubleValue());
				bookingDetails.setShowtime(resultList.get(0).getShowtime());
				flag=true;
			}
			else
				flag=false;
		}
		return flag;
	}

}
